package com.example.backend.dto;

import com.example.backend.domain.enums.Role;
import com.example.backend.domain.enums.UserStatus;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.Instant;

/**
 * 用户筛选条件
 * 支持多维度筛选和搜索
 */
@Data
public class UserFilter {
    // 基础筛选条件
    private Role role;
    private UserStatus status;
    
    // 文本搜索
    private String keyword;
    private String usernameKeyword;
    private String fullNameKeyword;
    private String emailKeyword;
    
    // 部门和组织筛选
    private String department;
    private String position;
    
    // 时间范围筛选
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant createdFrom;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant createdTo;
    
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant lastLoginFrom;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant lastLoginTo;
    
    // 登录统计筛选
    private Integer minLoginCount;
    private Integer maxLoginCount;
    
    // 排序相关
    private String sortBy = "createdAt";  // 默认按创建时间排序
    private String sortDirection = "DESC"; // 默认降序
    
    // 分页相关
    private Integer page = 0;
    private Integer size = 20;
    
    /**
     * 检查是否有有效的筛选条件
     */
    public boolean hasFilters() {
        return role != null || status != null ||
               keyword != null || usernameKeyword != null || fullNameKeyword != null || emailKeyword != null ||
               department != null || position != null ||
               createdFrom != null || createdTo != null ||
               lastLoginFrom != null || lastLoginTo != null ||
               minLoginCount != null || maxLoginCount != null;
    }
    
    /**
     * 检查是否有文本搜索
     */
    public boolean hasTextSearch() {
        return keyword != null || usernameKeyword != null || fullNameKeyword != null || emailKeyword != null;
    }
    
    /**
     * 检查是否有时间范围筛选
     */
    public boolean hasTimeRange() {
        return createdFrom != null || createdTo != null || 
               lastLoginFrom != null || lastLoginTo != null;
    }
}
